<?php

namespace App\Admin\Extensions;

class BaseAction
{
    protected $id;
    public $row;
    protected $actionUrl;
    protected $extra;

    public function __construct($id, $row, $url = '', $extra = [])
    {
        $this->id = $id;
        $this->row = $row;
        $this->actionUrl = $url;
        $this->extra = $extra;
    }

    protected function script($actionUrl = '', $html = '')
    {
        $confirm = '确认执行该操作么？';
        return <<<SCRIPT
        $(document).ready(function() {
            $('.grid-check-row').on('click', function (){
                var _this = this;
                var showHtml = $(_this).data('show-html');
                swal({
                    title: "{$confirm}",
                    html: showHtml==1 ? '{$html}' : '',
                    showCancelButton: true,
                    preConfirm: function() {
                        $.ajax({
                            type: "POST",
                            url: "/{$actionUrl}",
                            model: $(_this).data('model'),
                            data: {
                                id: $(_this).data('id'),
                                status: $(_this).data('status'),
                                extra: $(_this).data('extra'),
                                op:$(_this).data('op'),
                                action:$(_this).data('action'),
                                reason_type:$('#freeze_reason_type').val(),
                                reason:$('#freeze_reason').val(),
                                _method: 'POST',
                                _token: LA.token,
                            },
                            success: function (data) {
                                $.pjax.reload('#pjax-container');
                                if(data.ret==0) toastr.success(data.message);
                                else toastr.error(data.message);
                            }
                        });
                    }
                }, function(inputValue) {
                    if (inputValue === false) {
                        return false;
                    }
                    if (inputValue === "") {
                        swal.showInputError("内容不能为空！");
                        return false;
                    }
                    swal("Nice!", "你输入的是：" + inputValue, "success")
                });
            });
        });
SCRIPT;
        //         return <<<SCRIPT
        //         $(document).ready(function() {
        //             $('.grid-check-row').on('click', function (){
        //                 if(confirm("{$confirm}")) {
        //                     $.ajax({
        //                         type: "POST",
        //                         url: "/{$actionUrl}",
        //                         model: $(this).data('model'),
        //                         data: {
        //                             id: $(this).data('id'),
        //                             status: $(this).data('status'),
        //                             extra: $(this).data('extra'),
        //                             op:$(this).data('op'),
        //                             action:$(this).data('action'),
        //                             _method: 'POST',
        //                             _token: LA.token,
        //                         },
        //                         success: function (data) {
        //                             $.pjax.reload('#pjax-container');
        //                             if(data.ret==0) toastr.success(data.message);
        //                             else toastr.error(data.message);
        //                         }
        //                     });
        //                 }
        //             });
        //         });
        // SCRIPT;
    }

    protected function buildButton($type = 'ok', $text = '', $data = [])
    {
        $attr = '';
        if (!empty($data)) {
            foreach ($data as $field => $value) {
                if ($field == 'url') $attr .= " href='{$value}' target='_blank'";
                else $attr .= " data-{$field}='{$value}'";
            }
        }
        switch ($type) {
            case 'ok':
                return "<a class='btn btn-xs btn-success grid-check-row'  title='{$text}' {$attr}>{$text}</a>";
            case 'fail':
                return "<a class='btn btn-xs btn-danger grid-check-row'  title='{$text}' {$attr}>{$text}</a>";
            case 'link':
                return "<a class='btn btn-xs btn-success' title='{$text}' {$attr}>{$text}</a>";
        }
    }

    protected function render()
    {
        return '';
    }

    public function __toString()
    {
        return $this->render();
    }
}
